using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Domain.Entities;
using Domain.Services;
using UserInterface.Misc;

namespace UserInterface.PM
{
    public partial class Compose : BoardPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Menu.IsComposeActive = true;
            
            string pageTitle = (string)GetLocalResourceObject("Title");

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(pageTitle, "Compose.aspx");

            if (!IsPostBack)
            {
                string to = Request.QueryString["to"];
                if (to != null)
                {
                    ReceiverTextBox.Text = to;
                }
                
                SuccessPlaceHolder.Visible = false;
            }
        }

        protected void SendButton_Click(object s, EventArgs e)
        {
            string receiverUsername = ReceiverTextBox.Text;
            User receiver = UserService.GetByUsername(receiverUsername);
            if (receiver == null)
            {
                string notFoundMessage = String.Format(
                    (string)GetLocalResourceObject("ReceiverNotFoundMessage"),
                    receiverUsername
                );
                MasterPage.Messages.AddExclamation(notFoundMessage);
            }
            else if (receiver.Id == UITools.GuestUserId)
            {
                MasterPage.Messages.AddExclamation(
                    (string)GetLocalResourceObject("CannotMessageGuestUser")
                );
            }
            else
            {
                User sender = CurrentSession.User;
                string subject = SubjectTextBox.Text;
                string content = ContentTextBox.Text;
                PrivateMessageService.Send(sender, receiver, subject, content);
                FormPlaceHolder.Visible = false;
                SuccessPlaceHolder.Visible = true;
            }
        }
    }
}
